package ru.evga.gwt.lib.common.client.ui.dialog;

import ru.evga.gwt.lib.common.client.ui.editor.EditManager;

import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;


public abstract class AbstractEditDialog<T, E extends Editor<? super T>, W extends IsWidget>
    extends AbstractDialog
    implements EditButtonBar.Presenter
{
    private EditManager<T, E, W> editManager;
    
    public AbstractEditDialog()
    {
        super(true);
        editManager = new EditManager<T, E, W>();
    }

    protected void init(T data, E editor, W panel, EditManager.EditCallback<T> callback )
    {
        editManager.init(data, editor, panel, callback, getDriver());
    }

    abstract protected SimpleBeanEditorDriver<T, E> getDriver();

    public Widget createButtonBar()
    {
        EditButtonBar bar = new EditButtonBar();
        bar.setPresenter(this);
        return bar;
    }

    public void saveClicked()
    {
        editManager.save();
    }

    /**
     * Creates content
     */
    public IsWidget createContent()
    {
        return editManager.createContent();
    }

    public void cancelClicked()
    {
        editManager.cancel();
    }
    
    public T getEditedData()
    {
        return editManager.getEditedData();
    }
}
